# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Sertonix <sertonix@posteo.net>
pkgname=syncthing
pkgver=1.28.0
pkgrel=0
pkgdesc="Open Source Continuous File Synchronization"
url="https://syncthing.net/"
arch="all"
license="MPL-2.0"
pkgusers="$pkgname"
pkggroups="$pkgname"
makedepends="go"
install="$pkgname.pre-install"
subpackages="$pkgname-doc $pkgname-utils $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/syncthing/syncthing/archive/v$pkgver.tar.gz
	build-unset-CGO_ENABLED.patch
	only-test-with-race-when-provided.patch
	$pkgname.initd
	"
options="chmod-clean net"
case "$CARCH" in
# arm*: oom on tests
# riscv64: bind error
arm*|riscv64) options="$options !check" ;;
esac

# secfixes:
#   1.15.1-r0:
#     - CVE-2021-21404

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	# Build syncthing + server utils
	local cmd
	for cmd in syncthing stdiscosrv strelaypoolsrv strelaysrv; do
		echo "Compiling $cmd"
		go run build.go -no-upgrade -version=v$pkgver install $cmd
	done
}

check() {
	go test ./...
}

package() {
	local i

	install -d -o $pkgname -g $pkgname "$pkgdir"/var/lib/$pkgname
	install -D -m755 -t "$pkgdir"/usr/bin/ bin/*
	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname

	# Desktop files so people in Desktop Environments can put them on Autostart
	install -Dm0644 etc/linux-desktop/*.desktop -t "$pkgdir"/usr/share/applications

	for i in 32 64 128 256 512; do
		install -Dm644 "$builddir"/assets/logo-"$i".png \
			"$pkgdir"/usr/share/icons/hicolor/"$i"x"$i"/apps/$pkgname.png
	done
	install -Dm644 "$builddir"/assets/logo-only.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/$pkgname.svg

	# man pages
	for i in 1 5 7; do
		install -Dm644 -t "$pkgdir"/usr/share/man/man"$i"/ man/*."$i"
	done
}

utils() {
	pkgdesc="Syncthing server utilities"

	amove 'usr/bin/st*'
}

sha512sums="
53cbb04f260fb731f99b3653c9508794900dc3dc0593896cdd5a2bb499334c7db28f88fa148851ee0990bd319a670ec3e0628a9b2e5f99a5622414954b245738  syncthing-1.28.0.tar.gz
30c96641f073ba732d495e9df4b04a9f00ddb19439b4764c2aef07e4ec4a5f53ec6527210c76dc2d2af8f76a37666e44c57acd444206427a258c1b39b53ee5d0  build-unset-CGO_ENABLED.patch
81bcb6b2e0956624b596201d1de24a3b6fcb10d08761f2c426081350b611295a7f4d47775d175f2ee5dbbb289b98bc022389fc9992f0d31bcdbfde855ceafaf8  only-test-with-race-when-provided.patch
17da9c6e03efa3b43a4b46bfb85a7a63d393bbe5a8f431e4e6d86a6dcde6dbe0c536d7b29c007a1a5207534519dc87183f6f91e5596ea5e645cd702e4e2478b4  syncthing.initd
"
